package com.example.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson2.JSONObject;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.demo.databinding.ActivityGoldBinding;

import java.util.Date;
import java.util.Objects;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GoldActivity extends AppCompatActivity {
    private static final String TAG = "GoldActivity";

    private ActivityGoldBinding binding;

    private String url = "https://api.jisuapi.com/gold/shgold?appkey=f3557914adabf438";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityGoldBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        loadInfo();
        setJump();
    }

    private void setJump(){
        binding.jumpToAstro.setOnClickListener(view -> {
            startActivity(new Intent(this, AstroActivity.class));
        });
        binding.jumpToHis.setOnClickListener(view -> {
            startActivity(new Intent(this, DayInHistoryActivity.class));
        });
        binding.jumpToGold.setOnClickListener(view -> {
            startActivity(new Intent(this, GoldActivity.class));
        });
        binding.jumpToNews.setOnClickListener(view -> {
            startActivity(new Intent(this, NewsListActivity.class));
        });
        binding.jumpToPhone.setOnClickListener(view -> {
            startActivity(new Intent(this, PhoneActivity.class));
        });
    }


    private void loadInfo() {
        new Thread(() -> {
            try{
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder() .url(url) .build();
                Response response = client.newCall(request).execute();
                String body = response.body().string();

                GoldActivity.this.runOnUiThread(() -> {
                    Log.i(TAG, body);
                    JSONObject mapObj = JSONObject.parseObject(body);
                    JSONObject resultObj = mapObj.getJSONArray("result").getJSONObject(0);
                    binding.goldPrice.setText(resultObj.getString("price"));
                    binding.goldOpeningprice.setText(resultObj.getString("openingprice"));
                    binding.goldMaxprice.setText(resultObj.getString("maxprice"));
                    binding.goldMinprice.setText(resultObj.getString("minprice"));
                    binding.goldChangepercent.setText(resultObj.getString("changepercent"));
                });

            }catch (Exception e){
                Log.i(TAG, Objects.requireNonNull(e.getMessage()));
            }
        }).start();

    }

}